package com.iss.apartmentmanage.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.iss.apartmentmanage.entity.CheckinRecord;
import com.iss.apartmentmanage.model.BaseModel;
import com.iss.apartmentmanage.model.RecordWithRoom;
import com.iss.apartmentmanage.model.RecordWithRoomForAll;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface CheckinRecordMapper extends BaseMapper<CheckinRecord>{
    @Select("SELECT checkin_record.*,room_manage.id AS room_id FROM checkin_record INNER JOIN room_manage ON checkin_record.room_id" +
            "= room_manage.id WHERE checkin_record.id = #{id}")
    CheckinRecord selectGuestMessageByRoom(int id);
    @Select("SELECT room_manage.id AS roomid,room_manage.room_name,checkin_record.* FROM checkin_record INNER JOIN" +
            " room_manage ON checkin_record.room_id = room_manage.id LIMIT #{pageNum},#{pageSize}")
    List<RecordWithRoomForAll> selectAllCheckinRecord(Integer pageNum, Integer pageSize);

    @Select("<script> " +
            " select checkin_record.*,room_manage.* from room_manage inner join checkin_record on checkin_record.room_id=room_manage.id" +
            "   <where>" +
            "    room_id in(" +
            "    select id  from room_manage where 1=1 " +
            "    <if test =\"roomType!=null \">" +
            "      and room_type=#{roomType}" +
            "    </if>" +
            "    <if test =\"roomName!=null \">" +
            "      and room_name=#{roomName}" +
            "    </if>" +
            "    <if test =\"areaId!=null \">" +
            "      and area_id=#{areaId}" +
            "    </if>" +
            "    <if test =\" priceFrom!=null \">" +
            "      and  room_price BETWEEN #{priceFrom} and #{priceTo}" +
            "    </if>" +
            "    <if test =\"status!=null \">" +
            "      and status=#{status}" +
            "    </if>" +
            "           )" +
            "    </where>" +
            "   LIMIT #{pageNum},#{pageSize}" +
            "</script>")
    List<RecordWithRoomForAll> selectRoomForCheckOut (Integer pageNum, Integer pageSize, @Param("roomType") String roomType, @Param("roomName")String roomName,
                                               @Param("areaId") Integer areaId, @Param("priceFrom")Double priceFrom, @Param("priceTo")Double priceTo,
                                               @Param("status")Integer status);

    @Update("<script> update checkin_record set is_used=#{isUser} where id in " +
            "       <foreach collection=\"id\" item=\"id\" separator=\",\" open=\"(\" close=\")\">" +
            "          #{id}" +
            "       </foreach>" +
            "</script>")
    Integer updateIsUsedCheckinRecordByRoomIds(int[] id,int isUser);

}
